package com.example.Cairodio;

import java.io.File;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

/**
 * This class defines a gallery for the images taken by Cairodio application.
 * 
 * @author AnwaRowers
 * 
 */
public class MyGallery extends Activity {
	static File rootFile;
	static Uri[] uris;
	ImageAdapter imgAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gallery);

		GridView gridview = (GridView) findViewById(R.id.gridview);
		imgAdapter = new ImageAdapter(this);
		gridview.setAdapter(imgAdapter);

		gridview.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v,
					int position, long id) {
				Intent intent = new Intent().setClass(MyGallery.this,
						GalleryItemActivity.class);
				intent.putExtra("current", (int) position);
				intent.putExtra("max", (int) imgAdapter.getCount());
				startActivity(intent);
			}
		});

	}
}

/**
 * defines an Adapter for the gallery. It fetches the uri's that belong to the
 * application and pass them to be displayed.
 * 
 * @author AnwaRowers
 * 
 */
class ImageAdapter extends BaseAdapter {
	private Context mContext;
	private Uri[] images;
	private int number;

	public ImageAdapter(Context c) {
		mContext = c;
		populate();
		MyGallery.uris = images;
	}

	/**
	 * evaluate the number of files that will inhabit the gallery, to construct
	 * a convenient array for the Uri's in size.
	 * 
	 * @return
	 */
	public int getNumberOfFiles() {
		File rootFile = new File(Environment.getExternalStorageDirectory()
				+ File.separator + "DCIM" + File.separator + "Camera");
		File[] children = rootFile.listFiles();
		int counter = 0;
		int length = children.length;
		for (int i = 0; i < length; i++) {
			String s = children[i].getName();
			if (s.startsWith("Cairodio_")) {
				counter++;

			}
		}
		this.number = counter;
		return counter;
	}

	/**
	 * populate the array of Uri's with all the Uri's that belong to the
	 * application.
	 */
	public void populate() {
		images = new Uri[getNumberOfFiles()];
		int index = 0;
		String sep = File.separator;
		File rootFile = new File(Environment.getExternalStorageDirectory()
				+ File.separator + "DCIM" + File.separator + "Camera");
		File[] children = rootFile.listFiles();
		int length = children.length;
		for (int i = 0; i < length; i++) {
			String s = children[i].getName();
			if (s.startsWith("Cairodio_")) {
				Uri uri = Uri.parse(Environment.getExternalStorageDirectory()
						+ sep + "DCIM" + sep + "Camera" + sep
						+ (children[i]).getName());
				images[index] = uri;
				index++;
			}
		}
	}

	/**
	 * return the number of elements in the Uri list.
	 */
	public int getCount() {
		return number;
	}

	public Object getItem(int position) {
		return null;
	}

	public long getItemId(int position) {
		return 0;
	}

	/**
	 * create a new ImageView for each item referenced by the Adapter
	 */
	public View getView(int position, View convertView, ViewGroup parent) {
		ImageView imageView;
		if (convertView == null) { // if it's not recycled, initialize some
									// attributes
			imageView = new ImageView(mContext);
			imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
			imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
			imageView.setPadding(8, 8, 8, 8);
		} else {
			imageView = (ImageView) convertView;
		}
		if (position < number)
			imageView.setImageURI(images[position]);
		return imageView;
	}

}
